<template>
  <div class="article-container">
     <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- 文章详情 -->
    <div class="loading" v-if="loading">
      <van-loading class="inner" type="spinner" color="#1989fa" />
    </div>
    <div class="detail" v-else>
      <h3 class="title">{{article.title}}</h3>
      <div class="author">
        <van-image :src="article.aut_photo" round width="1rem" height="1rem" fit="fill" />
        <div class="text">
          <p class="name">{{article.aut_name}}</p>
          <p class="time">{{article.pubdate | fromNow }}</p>
        </div>
        <van-button
          round
          size="small"
          type="info"
          @click="isfollow"
        >{{article.is_followed ? '- 取消':'+ 关注'}}</van-button>
      </div>
      <div class="content">
        <div>正文</div>
      </div>

      <van-divider>END</van-divider>

      <div class="zan">
        <van-button
          round
          size="small"
          hairline type="primary"
          plain
          :icon="article.attitude === 1 ? 'good-job':'good-job-o'"
          @click="isLike"
        >
          {{article.attitude === 1 ? '取消点赞':'点赞'}}
        </van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          icon="delete"
          @click="isDisLike"
        >
            {{article.attitude === 0 ? '取消不喜欢':'不喜欢'}}
        </van-button>
      </div>
      <Comment></Comment>
    </div>
    <!-- /文章详情 -->
  </div>
</template>

<script>
import { followUser, getArticleDetail, unfollowUser, cancelLike, addLike, cancelDisLike, addDisLike } from '@/api/article'
import { mapState } from 'vuex'
import Comment from './comment'
export default {
  name: 'Article',
  components: {
    Comment
  },
  data () {
    return {
      loading: true,
      article: {}
    }
  },
  async created () {
    console.log('article组件创建了')
    const res = await getArticleDetail(this.id)
    // console.log(res)
    this.article = res.data
    this.loading = false
  },
  beforeDestroy () {
    console.log('article组件销毁了')
  },
  computed: {
    id () {
      return this.$route.params.id
    },
    ...mapState('user', ['token'])
  },
  methods: {
    async isfollow () {
      if (!this.token.token) {
        this.$router.push({
          path: '/login',
          query: {
            goBack: true
          }
        })
        this.$toast('请先登录')
      }
      if (this.article.is_followed) {
        await unfollowUser(this.article.aut_id)
      } else {
        await followUser(this.article.aut_id)
      }
      this.article.is_followed = !this.article.is_followed
      this.$toast('操作成功')
    },
    async isLike () {
      if (!this.token.token) {
        this.$router.push({
          path: '/login',
          query: {
            goBack: true
          }
        })
        this.$toast('请先登录')
      }
      if (this.article.attitude === 1) {
        await cancelLike(this.id)
        this.$toast('取消点赞')
      } else {
        await addLike(this.id)
        this.$toast('点赞成功')
      }
      this.article.attitude = this.article.attitude === 1 ? -1 : 1
    },
    async isDisLike () {
      if (!this.token.token) {
        this.$router.push({
          path: '/login',
          query: {
            goBack: true
          }
        })
        this.$toast('请先登录')
      }
      if (this.article.attitude === 0) {
        await cancelDisLike(this.id)
        this.$toast('取消不喜欢')
      } else {
        await addDisLike(this.id)
        this.$toast('不喜欢此篇文章')
      }
      this.article.attitude = this.article.attitude === 0 ? -1 : 0
    }
  }
}
</script>

<style lang='less' scoped>
.loading {
  height: 100vh;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}
.article-container{
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error{
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan{
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size:14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    ::v-deep img{
      max-width:100%;
      background: #f9f9f9;
    }
  }
}
</style>
